package com.xw.childhealth.controller;

import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.ChildrenHyjcmb;
import com.xw.childhealth.service.ChildrenHyjcmbService;
import com.xw.childhealth.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-15 09:41
 * @Version: 1.0
 */
@RestController
@RequestMapping("/childrenHyjcmb")
public class ChildrenHyjcmbController {

    @Autowired
    private ChildrenHyjcmbService childrenHyjcmbService;


    @PostMapping("/getChildrenHyjcmbList")
    public ResultVO getChildrenHyjcmbList() {
        List<ChildrenHyjcmb> all = childrenHyjcmbService.findAll();
        return ResultVOUtil.success(all);
    }


    @RequestMapping("/data-upd")
    public ModelAndView edit(@RequestParam(value = "type", required = false) String type,
                             @RequestParam(value = "code", required = false) String code,
                             @RequestParam(value = "id", required = false) Integer id,
                             @RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "category", required = false) String category,
                             @RequestParam(value = "isPublic", required = false) String isPublic,
                             Map<String, Object> map) {
        if (type == null) {
            return new ModelAndView("/pages/admin/children-hyjcmb", map);
        }
        if (type.equals("toUpd")) {
            ChildrenHyjcmb one = childrenHyjcmbService.findByIdEquals(id);
            map.put("type", type);
            map.put("result", one);
        } else if (type.equals("upd")) {
            ChildrenHyjcmb one = childrenHyjcmbService.findByIdEquals(id);
            childrenHyjcmbService.save(one.setCode(code).setCategory(category).setIsPublic(isPublic == null ? 0 : 1).setName(name).setCreateTime(new Date()).setUpdateTime(new Date()));
            return new ModelAndView("/pages/admin/children-hyjcmb", map);
        }
        if (type.equals("toAdd")) {
//            map.put("addRecordNode", addRecordNode);
        } else if (type.equals("add")) {
            childrenHyjcmbService.save(new ChildrenHyjcmb().setCode(code).setCategory(category).setIsPublic(isPublic == null ? 0 : 1).setName(name).setCreateTime(new Date()).setUpdateTime(new Date()));
            return new ModelAndView("/pages/admin/children-hyjcmb", map);
        }
        return new ModelAndView("/pages/admin/children-hyjcmb-data-add", map);
    }


    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        ChildrenHyjcmb result = childrenHyjcmbService.findByIdEquals(id);
        if (result != null) {
            childrenHyjcmbService.delRecord(id);
        }
        return new ModelAndView("/pages/admin/children-hyjcmb");
    }
}
